/**
 * BatteryLogServiceImpl.java
 * Created at 2017-11-13
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.service.impl;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.rms.system.dao.IBatteryLogDao;
import com.broadtext.rms.system.model.BatteryLog;
import com.broadtext.rms.system.service.IBatteryLogService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: BatteryLogServiceImpl</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-11-13</p>
 */
@Service("batteryLogService")
public class BatteryLogServiceImpl extends EntityServiceImpl<BatteryLog> implements IBatteryLogService{
    
    /**
     * 注入Dao
     */
    @Autowired
    @Qualifier("batteryLogDao")
    private IBatteryLogDao batteryLogDao;
    
    @Autowired
    @Qualifier("batteryLogDao")
    public void setEntityDao(EntityDao<BatteryLog> entityDao) {
        super.entityDao = entityDao;
    }

    /** (non-Javadoc)
     * <p>Title: findCusNameOpt</p>
     * <p>Description: 其他相关的数据库查询</p>
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IBatteryLogService#findCusNameOpt()
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Object> findCusNameOpt() throws ServiceException {
        List<Object> list = new ArrayList<Object>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.BatteryLog.findCusNameOpt", null);
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return list;
    }

    /** (non-Javadoc)
     * <p>Title: getRuntimeLog</p>
     * <p>Description: 查询电池运行日志分页</p>
     * @param map
     * @param pageBounds
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IBatteryLogService#getRuntimeLog(java.util.Map, com.github.miemiedev.mybatis.paginator.domain.PageBounds)
     */
    @Override
    public EntityPageBean<Object> getRuntimeLog(Map<String, Object> map, PageBounds pageBounds) throws ServiceException {
        try {
            return this.entityDao.findObjectEntityPage("com.broadtext.rms.system.model.BatteryLog.getRuntimeLog", map, pageBounds);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /** (non-Javadoc)
     * <p>Title: getRuntimeLog</p>
     * <p>Description: 查询电池运行日志分页</p>
     * @param map
     * @param pageBounds
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IBatteryLogService#getRuntimeLog(java.util.Map, com.github.miemiedev.mybatis.paginator.domain.PageBounds)
     */
    @Override
    public EntityPageBean<Object> getBatteryChargeLog2(Map<String, Object> map, PageBounds pageBounds) throws ServiceException {
        try {
            return this.entityDao.findObjectEntityPage("com.broadtext.rms.system.model.BatteryLog.getBatteryChargeLog2", map, pageBounds);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /** (non-Javadoc)
     * <p>Title: getRuntimeLog</p>
     * <p>Description: 查询电池充电日志分页</p>
     * @param map
     * @param pageBounds
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IBatteryLogService#getRuntimeLog(java.util.Map, com.github.miemiedev.mybatis.paginator.domain.PageBounds)
     */
    @SuppressWarnings({ "unchecked"})
    @Override
    public List<Object> getBatteryChargeLog(Map<String, Object> map) throws ServiceException {
        try {
            List<Object> list =  this.entityDao.findByProperty("com.broadtext.rms.system.model.BatteryLog.getBatteryChargeLog", map);
            return list;
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /** (non-Javadoc)
     * <p>Title: getRuntimeLog</p>
     * <p>Description: 查询电池充电日志分页</p>
     * @param map
     * @param pageBounds
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IBatteryLogService#getRuntimeLog(java.util.Map, com.github.miemiedev.mybatis.paginator.domain.PageBounds)
     */
    @Override
    public long getRecordsTotal(Map<String, Object> map) throws ServiceException {
        long count = 0;
        try {
            count = this.batteryLogDao.getRecordsTotal(map);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return count;
    }
    
    /** (non-Javadoc)
     * <p>Title: findExportDataList</p>
     * <p>Description: </p>
     * @param map
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IAlertQueryService#findExportDataList(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, Object>> findExportDataList(Map<String, Object> map) throws ServiceException {
        List<Map<String, Object>> list;
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.BatteryLog.findExportDataList", map);
            for (Map<String, Object> map2 : list) {
                long totalRuntime = Long.valueOf(map2.get("totalRuntime")+"");
                if(totalRuntime>0){
                    long hour = totalRuntime/3600;
                    long minutes = (totalRuntime%3600)/60;
                    long seconds = (totalRuntime%3600)%60;
                    map2.put("totalRuntimeStr", hour+"小时"+minutes+"分钟"+seconds+"秒");
                }else {
                    map2.put("totalRuntimeStr", "");
                }
                /*long bmsAlertNum = Long.valueOf(map2.get("bmsAlertNum")+"");
                long customAlertNum = Long.valueOf(map2.get("customAlertNum")+"");
                map2.put("totalAlertNum", bmsAlertNum+customAlertNum);*/
            }
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return list;
    }
    
    /** (non-Javadoc)
     * <p>Title: findExportDataList</p>
     * <p>Description: </p>
     * @param map
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.system.service.IAlertQueryService#findExportDataList(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    @Override
    public List<Map<String, Object>> findChargeLogDataList(Map<String, Object> map) throws ServiceException {
        List<Map<String, Object>> list;
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.system.model.BatteryLog.findChargeLogDataList", map);
            for (Map<String, Object> map2 : list) {
                long totalRuntime = Long.valueOf(map2.get("totalChargeTime")+"");
                if(totalRuntime>0){
                    long hour = totalRuntime/3600;
                    long minutes = (totalRuntime%3600)/60;
                    long seconds = (totalRuntime%3600)%60;
                    map2.put("totalChargeTimeStr", hour+"小时"+minutes+"分钟"+seconds+"秒");
                }else {
                    map2.put("totalChargeTimeStr", "");
                }
                double socStart = Double.valueOf(map2.get("socStart")+"");
                DecimalFormat df = new DecimalFormat("######0.00");
                String socStartStr = df.format(socStart)+"%";
                map2.put("socStartStr", socStartStr);
                double socEnd = Double.valueOf(map2.get("socEnd")+"");
                String socEndStr = df.format(socEnd)+"%";
                map2.put("socEndStr", socEndStr);
            }
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return list;
    }
    


}
